package _2022.hot100._437_路径总和III;

/**
 * @author： YHSimon
 * @date： 2022-06-06 10:42
 */
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    @Override
    public String toString() {
        return this.val + "";
    }
}

public class Solution {
    public int ans;

    public int pathSum(TreeNode root, int targetSum) {
        ans = 0;
        dfs(root, targetSum);
        return ans;

    }

    public void dfs(TreeNode root, int targetSum) {
        if (root == null) {
            return;
        }
        dfs(root, targetSum, 0);
        dfs(root.left, targetSum);
        dfs(root.right, targetSum);

    }

    public void dfs(TreeNode root, int targetSum, int curSum) {
        if (root == null) {
            return;
        }
        curSum += root.val;
        if (curSum == targetSum) {
            ans++;
        }
        dfs(root.left, targetSum, curSum);
        dfs(root.right, targetSum, curSum);
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        // System.out.println(s.pathSum(new TreeNode(10,new TreeNode(5,new TreeNode(3,new TreeNode(3),new TreeNode(-2)),new TreeNode(2,null,new TreeNode(1))),new TreeNode(-3,null,new TreeNode(11))), 8));
        System.out.println(s.pathSum(new TreeNode(1, null, new TreeNode(2, null, new TreeNode(3, null, new TreeNode(4)))), 3));
    }
}
